﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
using Catalina.Common;
namespace Catalina.Dal
{
	public class CatalinaLabelPersister
	{
		public CatalinaLabelPersister(string corpConnectionString, string storeConnectionString)
		{
			this.corpConnectionString = corpConnectionString;
			this.storeConnectionString = storeConnectionString;
		}

		public void BulkInsert()
		{
			String selectSQL = "SELECT * FROM catalina_signs";

			using (SqlConnection connection = new SqlConnection(storeConnectionString))
			{
				using (SqlCommand command = new SqlCommand("TRUNCATE TABLE POSplus_catalina_signs", connection))
				{
					connection.Open();
					try
					{
						command.ExecuteNonQuery();
					}
					catch { }
					connection.Close();
				}
			}

			using(SqlConnection connection = new SqlConnection(corpConnectionString))
			{
				using(SqlCommand command = new SqlCommand(selectSQL, connection))
				{
					connection.Open();
					SqlDataReader reader = command.ExecuteReader();

					SqlBulkCopy sbc = new SqlBulkCopy(storeConnectionString);
					sbc.DestinationTableName = "POSplus_catalina_signs";
					sbc.WriteToServer(reader);
					sbc.Close();
					reader.Close();
					connection.Close();
				}
			}
		}

		private string storeConnectionString;
		private string corpConnectionString;
	}
}
